Skip to content

feat(flags): support passing in lists of flag keys to the /flags endpoint instead of evaluating every flag every time we fall back#307

Merged
dmarticus merged 4 commits intomasterfrom
feat/evaluate-single-flag-keys
Aug 18, 2025
Merged

feat(flags): support passing in lists of flag keys to the /flags endpoint instead of evaluating every flag every time we fall back#307
dmarticus merged 4 commits intomasterfrom
feat/evaluate-single-flag-keys

Conversation

@dmarticus
Copy link
Contributor

@dmarticus dmarticus commented Aug 15, 2025

  1. Added flag_keys_to_evaluate parameter to all relevant feature flag methods:
  2. Added flag_keys_filter option to the send_feature_flags parameter in capture events, allowing selective flag evaluation
  3. Optimized local evaluation to filter flags based on flag_keys_to_evaluate when provided
  4. Updated single flag evaluation (_get_feature_flag_details_from_flags) to only request the specific flag being evaluated

Context: https://posthog.slack.com/archives/C06UTAJJEHM/p1755274454897179

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR introduces selective feature flag evaluation optimization to the PostHog Python SDK. The key change adds a flag_keys_to_evaluate parameter to all feature flag evaluation methods (get_flags_decision(), get_all_flags(), get_all_flags_and_payloads(), get_feature_variants(), get_feature_payloads(), and get_feature_flags_and_payloads()). This parameter allows clients to specify exactly which feature flags they want to evaluate instead of evaluating all available flags.

The implementation adds a flag_keys_filter option to the send_feature_flags parameter in capture events, enabling selective flag evaluation during event tracking. The optimization works at two levels: for remote evaluation, it passes the specified flag keys to the /flags endpoint to reduce server-side processing and network overhead; for local evaluation, it filters the flag processing to only evaluate the requested flags.

The change also optimizes single flag evaluation by making _get_feature_flag_details_from_decide request only the specific flag being evaluated rather than all flags. This addresses performance concerns for applications with many feature flags where only a subset needs evaluation. The implementation maintains backward compatibility by making the parameter optional with a default of None, preserving existing behavior when not specified.

Confidence score: 4/5

  • This PR is generally safe to merge but requires careful review of the parameter passing logic
  • Score reflects well-structured implementation with comprehensive tests, but the complex parameter threading across multiple methods introduces potential for subtle bugs
  • Pay close attention to posthog/client.py for proper parameter handling and the test files for coverage validation

6 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@dmarticus dmarticus requested a review from a team August 15, 2025 22:49
@dmarticus dmarticus moved this to In Review in Feature Flags Aug 15, 2025
@haacked
Copy link
Collaborator

haacked commented Aug 18, 2025

I was a little confused by flag_keys_filter. I expected it to be a predicate used to filter the keys. But I don't see it actually being used in the code. Is it only sent to the server to evaluate?

@dmarticus
Copy link
Contributor Author

I was a little confused by flag_keys_filter. I expected it to be a predicate used to filter the keys. But I don't see it actually being used in the code. Is it only sent to the server to evaluate?

It's part of the SendFeatureFlagsOptions object, it is user-configured and used as part of send_feature_flags when associated with capture.

@dmarticus dmarticus requested a review from haacked August 18, 2025 23:00
@github-project-automation github-project-automation bot moved this from In Review to Approved in Feature Flags Aug 18, 2025
@dmarticus dmarticus merged commit 20b8825 into master Aug 18, 2025
11 checks passed
@dmarticus dmarticus deleted the feat/evaluate-single-flag-keys branch August 18, 2025 23:10
@github-project-automation github-project-automation bot moved this from Approved to Done in Feature Flags Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants